// component/login/login.js
var app = getApp();
var api_token;
var code;
var userInfo;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    token: {
      type: String,
      value: null,
      observer: function (newVal, oldVal) {
        this.checkSession();
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showLoginPage: false,
    showLoginBtn: false,
    loginCli: false,  //点击登录
  },

  attached: function() {
    this.showTimeout = setTimeout(() => {
      this.setData({
        showLoginPage: true
      })
      wx.setNavigationBarColor({
        frontColor:'#ffffff',
        backgroundColor: '#1f1f1f'
      })
    }, 1000);
  },

  detached: function() {
    clearTimeout(this.showTimeout);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //检查微信session的登录状态
    checkSession: function () {
      //检查登录状态
      wx.checkSession({
        success: (res) => {
          //微信session 未过期, 继续检查login_token是否有效
          this.checkLoginToken()
        },
        fail: (err) => {
          //登录态过期,重新登录
          this.userLoginFail()
        }
      })
    },

    //检查login_token是否有效
    checkLoginToken: function () {
      var login_token = wx.getStorageSync("login_token") || '';
      if (login_token == '') {
        this.userLoginFail();
        return;
      }

      app.Ajax({
        // methods: 'POST',
        url: '/api/index.php?app=wechat&act=checkSession',
        data: {
          api_token: login_token
        },
        success: (res) => {
          if (res.is_login) {
            app.globalData.login_token = login_token;
            this.triggerEvent('success', {}, {})
          } else {
            this.userLoginFail();
          }
        },
        fail: (err, code) => {
          if (code == -1) {
            this.setData({
              message: err,
            })
          }
          //login_token验证接口调用失败,重新登录
          this.userLoginFail()
        }
      });
    },

    //用户登录失败
    userLoginFail: function () {
      this.setData({
        showLoginBtn: true,
        loginCli: false,
      })

      wx.login({
        success: (res) => {
          if (true) {
            code = res.code;
          }
        }
      })
    },

    //获取微信的用户信息
    getUserInfo: function (e) {
      //限制重复点击
      if (this.data.loginCli == true) {
        return;
      }

      this.setData({
        loginCli: true,
      })

      userInfo = e.detail;
      this.getUserToken();
    },

    //获取用户token
    getUserToken: function () {
      //服务器用户登录
      app.Ajax({
        url: '/api/index.php?app=wechat&act=miniProgramKey',
        data: {
          code: code
        },
        success: (res) => {
          console.log(res)
          api_token = res.api_token
          this.userLogin();
        },
        fail: (err, code) => {
          if (code == -1) {
            this.setData({
              message: err,
            })
          } else {
            this.setData({
              message: '抱歉，未能获取您的用户信息，请再试一次',
            })
          }
          this.userLoginFail();
        }
      });
    },

    /**
     * 用户授权登录
     */
    userLogin: function () {
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=wechat&act=oauthMiniProgram',
        data: {
          api_token: api_token,
          encryptedData: userInfo.encryptedData,
          iv: userInfo.iv,
          share_token: this.data.token
        },
        success: (res) => {
          if (res) {
            wx.setStorageSync('login_token', res.api_token);
            app.globalData.login_token = res.api_token;
            this.triggerEvent('success', {}, {})
          } else {
            this.setData({
              message: '抱歉，您的授权信息为空，请再试一次',
            })
            this.userLoginFail();
          }
        },
        fail: (err, code) => {
          if (code == -1) {
            this.setData({
              message: err,
            })
          } else {
            this.setData({
              message: '抱歉，未能获取您的用户授权，请再试一次',
            })
          }
          this.userLoginFail();
        }
      });
    },
  }
})
